package com.demo.deadLock;

import java.util.ArrayList;
import java.util.List;

/**
 * 分配器
 */
public class Allocator {

	private final List<Object> als = new ArrayList<>();

	/**
	 * 一次性申请所有资源
	 *
	 * @param from
	 * @param to
	 * @return
	 */
	synchronized boolean apply(Object from, Object to) {
		if (als.contains(from) || als.contains(to)) {
			return false;
		} else {
			als.add(from);
			als.add(to);
		}
		return true;
	}

	synchronized void clean(Object from, Object to) {
		als.remove(from);
		als.remove(to);
	}

	private void Allocator() {
	}

	private static class SingleTonHolder {
		private static final Allocator INSTANCE = new Allocator();
	}

	public static Allocator getInstance() {
		return SingleTonHolder.INSTANCE;
	}
}
